package com.yyf.camera_product_plugin.ble;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.yyf.camera_product_plugin.camera_product_plugin.R;

import java.util.ArrayList;
import java.util.List;

import cn.com.heaton.blelibrary.bluemanager.bean.WifiInfo;

public class WifiListDialog extends Dialog {
    public static final int CONNECT_WIFI = 100112;
    RecyclerView recyclerView;
    WifiListAdapter wifiListAdapter;


    TextView wifiSsidTv;
    EditText wifiPdEt;

    Activity activity;
    public WifiListDialog(@NonNull Activity context) {
        super(context);
        this.activity=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_blue_list);
        setCanceledOnTouchOutside(false);

        TextView title = findViewById(R.id.dialog_title);
        title.setText("点击WIFI名称选择要连接的WIFI，然后输入密码点击连接");

        recyclerView = findViewById(R.id.recycler);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

        findViewById(R.id.connect_wifi_ll).setVisibility(View.VISIBLE);
        wifiSsidTv = findViewById(R.id.wifi_ssid);
        wifiPdEt = findViewById(R.id.wifi_pw_et);
        findViewById(R.id.connect_wifi).setOnClickListener(view -> {
            if (selectWifiInfo != null) {
                selectWifiInfo.password = wifiPdEt.getText().toString();
//                ((MainActivity)activity).connectWifi(selectWifiInfo);
                dismiss();
            } else {
                Toast.makeText(getContext(), "请先选择要连接的wifi并输入密码", Toast.LENGTH_SHORT).show();
            }
        });
    }

    WifiInfo selectWifiInfo;

    List<WifiInfo> dateList = new ArrayList<>();

    public void setDate(WifiInfo wifi) {
        dateList.add(wifi);
        if (wifiListAdapter == null) {
            wifiListAdapter = new WifiListAdapter(getContext(), dateList);
            wifiListAdapter.setOnItenClickListener(wifiInfo -> {
                wifiSsidTv.setText("请输入'" + wifiInfo.ssid + "'的密码");
                selectWifiInfo = wifiInfo;
            });
            recyclerView.setAdapter(wifiListAdapter);
        } else {
            wifiListAdapter.setDlist(dateList);
            wifiListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void show() {
        super.show();
        selectWifiInfo = null;
        if (wifiSsidTv != null) {
            wifiSsidTv.setText("");
        }
        if (wifiPdEt != null) {
            wifiPdEt.setText("");
        }
    }
}
